// component/ThreeLevelLinkage.js
const api = require("../../util")
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    region: {  // .wxml文件里自定义的名称
      type: Array,  // 数据类型
      value: '',  // 默认值  
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 新地址
    cityArray: [],
    region: [],
    citysIndex: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取默认省市区
    default: function () {
      var that = this
      // 获取省
      api.http('/common/getProvinceList', 'POST', false, {
      }, function (res) {
        if (res.code == 200) {
          var cityArray = that.data.cityArray
          cityArray.push(res.data)
          that.setData({
            cityArray: cityArray,
          }, () => {
            // 获取市
            api.http('/common/getAreaList', 'POST', false, {
              province_id: res.data[0].id,
            }, function (resa) {
              if (resa.code == 200) {
                var cityArray = that.data.cityArray
                cityArray.push(resa.data)
                that.setData({
                  cityArray: cityArray,
                }, () => {
                  // 获取区
                  // api.http('/api/store/ssq_ld', 'POST', false, {
                  //   id: resa.data[0].id,
                  // }, function (resaa) {
                  //   if (resaa.code == 200) {
                  //     var cityArray = that.data.cityArray
                  //     cityArray.push(resaa.data)
                  //     that.setData({
                  //       cityArray: cityArray,
                  //     })
                  //   } else {
                  //     wx.showToast({
                  //       title: resaa.msg,
                  //       icon: 'none',
                  //       duration: 1500,
                  //     })
                  //   }
                  // })
                })
              } else {
                wx.showToast({
                  title: resa.msg,
                  icon: 'none',
                  duration: 1500,
                })
              }
            })
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
          })
        }
      })
    },
    // 监测滑动动态获取对应市区
    changeCitysChangeColumn: function (e) {
      var that = this
      var index_l = e.detail.value
      var column = e.detail.column
      if (column == 0) {
        // 获取市
        api.http('/common/getAreaList', 'POST', false, {
          province_id: that.data.cityArray[0][index_l].id,
        }, function (resa) {
          if (resa.code == 200) {
            var cityArray = "cityArray[" + 1 + "]"
            that.setData({
              [cityArray]: resa.data,
            }, () => {
              // 获取区
              // api.http('/api/store/ssq_ld', 'POST', false, {
              //   id: resa.data[0].id,
              // }, function (resaa) {
              //   if (resaa.code == 200) {
              //     var cityArray = "cityArray[" + 2 + "]"
              //     that.setData({
              //       [cityArray]: resaa.data,
              //     })
              //   } else {
              //     wx.showToast({
              //       title: resaa.msg,
              //       icon: 'none',
              //       duration: 1500,
              //     })
              //   }
              // })
            })
          } else {
            wx.showToast({
              title: resa.msg,
              icon: 'none',
              duration: 1500,
            })
          }
        })
      } else if (column == 1) {
        // 获取区
        // api.http('/api/store/ssq_ld', 'POST', false, {
        //   type: 3,
        //   id: that.data.cityArray[1][index_l].id,
        // }, function (resaa) {
        //   if (resaa.code == 200) {
        //     var cityArray = "cityArray[" + 2 + "]"
        //     that.setData({
        //       [cityArray]: resaa.data,
        //     })
        //   } else {
        //     wx.showToast({
        //       title: resaa.msg,
        //       icon: 'none',
        //       duration: 1500,
        //     })
        //   }
        // })
      }
    },
    // 确认选择
    func_changeCitysChange: function (e) {
      var that = this
      var cityArray = that.data.cityArray
      var index = e.detail.value
      that.setData({
        citysIndex: index,
        region: [cityArray[0][index[0]].name, cityArray[1][index[1]].name],
      })
      that.triggerEvent('changeAddress', {
        region: [cityArray[0][index[0]].name, cityArray[1][index[1]].name],
        region_id: [cityArray[0][index[0]].id, cityArray[1][index[1]].id],
      })
    },
  },
  // 组件布局完成
  ready: function () {
    var that = this
    that.default();
  }
})
